<template>
	<scroll-view scroll-y @scrolltolower="getList" @refresherrefresh="getList(true)" :refresher-enabled="true"
		:refresher-triggered="refresherTriggered">
		<view class="waybill-box">
			<waybill-item class="waybill-item" @refresherList="getList(true)" v-for="(item,index) in list" :items="item"
				:key="index"></waybill-item>
			<view class="get-more" v-show="getMoreStatus">
				<text>正在加载更多。。。</text>
			</view>
			<empty-waybill v-show="list.length==0"></empty-waybill>
		</view>
	</scroll-view>
</template>

<script>
	import emptyWaybill from "@/components/tool-empty/waybill";
	import waybillItem from "@/components/tool-card/waybill-item"
	export default {
		components: {
			waybillItem,
			emptyWaybill
		},
		props: {
			// 0：全部，1：已创建，2：运输中，3：已完成
			type: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				refresherTriggered: false,
				getMoreStatus: false,
				list: [],
				timer: null,
				pages: {
					size: 10, // 每次加载的数量
					current: 1, // 记录当前页
					total: '', // 记录总数
				}
			};
		},
		computed: {
			refuseMore() {
				// 计算是否还有更多
				return this.pages.total === this.list.length;
			},
		},
		methods: {
			// 加载，刷新
			getList(isRefresh = false) {
				if (this.timer) {
					return false;
				}

				if (isRefresh === true) {
					this.list = [];
					this.refresherTriggered = true;
					this.pages.current = 1;
					this.pages.total = 0;
				} else {
					if (this.refuseMore) {
						return false;
					}
					this.getMoreStatus = true;
					this.pages.current++;
				}
				// console.log(this.$store.state.userInfo)
				const formData = Object.assign({
					personType: this.$store.getters.getUserType,
					empInfoId: this.$store.getters.getEmpInfoId
				}, this.pages);
				if (this.$store.getters.getUserType == 0 && this.type == 0) {
					formData.approvalState = 1
				} else {
					formData.type = this.type == 0 ? '' : this.type;
				}
				this.timer = true;
				this.$api.getWaybillList(formData).then((res) => {
					this.timer = false;
					const resData = res.data.data;
					// console.log(resData)
					if (isRefresh==true) {
						this.list = resData.records;
					} else {
						this.list = this.list.concat(resData.records);
					}
					this.pages.total = resData.total;
					this.$emit('input', resData.total); // 修改父页面的角标数据
					this.refresherTriggered = false;
					this.getMoreStatus = false
				}).catch(() => {
					this.timer = false;
					this.refresherTriggered = false;
					this.getMoreStatus = false
				})
			}
		},
		mounted() {
			// 页面初始化，请求一次数据
			this.getList(true);
		},
	}
</script>

<style lang="scss" scoped>
	.waybill-box {
		padding: 20rpx 0 84rpx;
		position: relative;

		.waybill-item {
			margin-bottom: 20rpx;
		}

		.get-more {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			text-align: center;
			line-height: 3em;
			font-size: 28rpx;
			color: #666666;
		}
	}
</style>
